/**
 * 
 */
package com.wisdytech.linkdcs.equipment.service.impl;

//import static org.mockito.Matchers.matches;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.linkdcs.equipment.dao.IEqCollectionClientDao;
import com.wisdytech.linkdcs.equipment.dao.IEqCollectionParamDao;
import com.wisdytech.linkdcs.equipment.dao.IEqModelDao;
import com.wisdytech.linkdcs.equipment.model.CollectionClient;
import com.wisdytech.linkdcs.equipment.model.CollectionParam;
import com.wisdytech.linkdcs.equipment.model.EquipModel;
import com.wisdytech.linkdcs.equipment.service.IEquipmentParamViewService;

/**
 * <p>
 * ClassName: EquipmentParamViewService
 * </p>
 * <p>
 * Description: 可视化管理页面业务层
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-27
 * </p>
 */
@Service("equipmentParamViewService")
@Transactional
public class EquipmentParamViewServiceImpl extends BaseService<IEqCollectionParamDao, CollectionParam> implements IEquipmentParamViewService {
    
    /**
     * 注入eqCollectionParamDao
     */
    @Resource
    private IEqCollectionParamDao eqCollectionParamDao;
    /**
     * 注入eqModelDao
     */
    @Resource
    private IEqModelDao eqModelDao;
    /**
     * 注入eqCollectionClientDao
     */
    @Resource
    private IEqCollectionClientDao eqCollectionClientDao;
    
    @Override
    public PageInfo<CollectionParam> listParamViewByFilter(String modelCode, String showLevel, String modelType, Filter filter) {
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        queryMap.put("modelCode", modelCode);
        queryMap.put("modelType", modelType);
        queryMap.put("showLevel", showLevel);
        List<CollectionParam> lists =
                super.beforeList(IEqCollectionParamDao.class, filter).selectViewParamPage(queryMap);
        PageInfo<CollectionParam> equipModelPage=new PageInfo<>(lists);
        return equipModelPage;
    }

    @Override
    public StandardResult addCollectionParamView(CollectionParam param, String loginName) {
        EntityWrapper<CollectionParam> ew = new EntityWrapper<>();
        EquipModel model = new EquipModel();
        model = eqModelDao.getEuipmentModelByEmModel(param.getModelCode());
        CollectionClient client = new CollectionClient();
        client = eqCollectionClientDao.getCollectionClientByModelId(model.getModelCode());
        //1---实时分析参数
        if ("1".equals(param.getShowLevel())) {
            List<CollectionParam> paramList = eqCollectionParamDao.selectList(ew.eq("SHOW_LEVEL", param.getShowLevel()).eq("COLLECTION_CLIENT_ID", client.getPkId()));
            if (paramList.size()>=3) {
                return StandardResult.getFailResult(CommonConstants.COLLECTION_PARAM_SHOWLEVEL);
            }
        }
        param.setCollectionClientId(client.getPkId());
        //前台获取到参数id并暂时保存到参数id中
        param.setPkId(param.getParamId());
        param.setCreateDate(new Date());
        param.setCreateUser(loginName);
        eqCollectionParamDao.updateShowLevel(param);
        return StandardResult.getSuccessResult();
    }

    @Override
    public StandardResult delCollectionParamView(String id) {
        String[] ids = id.split(",");
        for (String pkId : ids) {
            CollectionParam collectParam = eqCollectionParamDao.selectById(pkId);
            //0--代表不是可视化参数
            collectParam.setShowLevel("0");
            eqCollectionParamDao.updateById(collectParam);
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public StandardResult updateCollectionParamView(CollectionParam param, String loginName) {
        EntityWrapper<CollectionParam> ew = new EntityWrapper<>();
        EquipModel model = new EquipModel();
        model = eqModelDao.getEuipmentModelByEmModel(param.getModelCode());
        CollectionClient client = new CollectionClient();
        client = eqCollectionClientDao.getCollectionClientByModelId(model.getModelCode());
        //1---实时分析参数
        if ("1".equals(param.getShowLevel())) {
            List<CollectionParam> paramList = eqCollectionParamDao.selectList(ew.eq("SHOW_LEVEL", param.getShowLevel())
                    .eq("COLLECTION_CLIENT_ID", client.getPkId()));
            int size = paramList.size();
            for (CollectionParam collectionParam : paramList) {
                if (param.getPkId().equals(collectionParam.getPkId())) {
                    size--;
                }
            }
            if (size >= 3) {
                return StandardResult.getFailResult(CommonConstants.COLLECTION_PARAM_SHOWLEVEL);
            }
        }
//        param.setEcId(client.getPkId());
        param.setUpdateUser(loginName);
        param.setUpdateDate(new Date());
        int count = eqCollectionParamDao.updateById(param);
        if (count <= 0) {
            throw new ServiceException("修改失败");
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public CollectionParam getEuipmentParamViewById(String pkId) {
        return eqCollectionParamDao.selectById(pkId);
    }

    @Override
    public List<CollectionParam> getEuipmentParamView(String pkId, String showLevel) {
        
        List<CollectionParam> params =new ArrayList<CollectionParam>();
        
        //设备id 不能为空
        if(StringUtils.isBlank(pkId)){
            return params;
        }
        
        //显示等级 不能为空
        if(StringUtils.isBlank(showLevel)){
            return params;
        }
               
        params =eqCollectionParamDao.selectParamView(pkId, showLevel);
        
        return params;
    }



}
